package com.tangyuan.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.model.User;
import com.tangyuan.model.activity.TicketType;
import com.tangyuan.user.mapper.TicketTypeMapper;
import com.tangyuan.user.service.ITicketTypeService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: ticket_type
 * @Author: jeecg-boot
 * @Date: 2024-03-20
 * @Version: V1.0
 */
@Service
public class TicketTypeServiceImpl extends ServiceImpl<TicketTypeMapper, TicketType> implements ITicketTypeService {


    @Override
    public Result<Object> addTicketType(Long userId, TicketType ticketType) {
        ticketType.setCreateTime(new Date());
        ticketType.setUserId(userId);
        this.save(ticketType);
        return Result.ok("操作成功！");
    }

    @Override
    public Result<Object> getTicketTypes(Long userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<TicketType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TicketType::getIsDeleted, 0);
        queryWrapper.eq(TicketType::getUserId, userId);
        queryWrapper.orderByDesc(TicketType::getCreateTime);
        List<TicketType> list = this.list(queryWrapper);
        return Result.ok(new PageInfo<>(list));
    }

    @Override
    public Result<Object> exitTicketType(Long userId, TicketType ticketType) {
        TicketType type = this.getById(ticketType.getId());
        if (!userId.equals(type.getUserId())) {
            return Result.build(null, ResultCodeEnum.YOU_HAVE_NO_CHANGE);
        }
        this.updateById(ticketType);
        return Result.ok("操作成功！");
    }

    @Override
    public Result<Object> deleteTicketType(Long userId, TicketType ticketType) {
        TicketType type = this.getById(ticketType.getId());
        if (!userId.equals(type.getUserId())) {
            return Result.build(null, ResultCodeEnum.YOU_HAVE_NO_CHANGE);
        }
        this.removeById(ticketType.getId());
        return Result.ok("操作成功！");
    }
}
